package com.zdzx.common.util;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;

public class WordPdfUtils {
	
	public boolean wordConverterToPdf(String docxPath,String resultPath) throws IOException {
	    File file = new File(docxPath);
	    String path = file.getParent();
	    try {
	        String osName = System.getProperty("os.name");
	        System.out.println(osName);
	        String command = "";
	        if (osName.contains("Windows")) {
	        	//soffice --convert-to pdf  -outdir E:/test.docx
	            command = "soffice --convert-to pdf  -outdir " + path + " " + docxPath;
	        } else {
	            command = "libreoffice6.4 --headless --invisible --convert-to pdf:writer_pdf_Export " + path + File.separator + file.getName().replaceAll(".(?i)docx", ".pdf") + " " + docxPath;
	        }
	        String result = CommandExecute.executeCommand(command);
//	        LOGGER.info("result==" + result);
	        System.out.println("生成pdf的result==" + result);
	        if (result.equals("") || result.contains("writer_pdf_Export")) {
	            return true;
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	        throw e;
	    }
	    return false;
	}
	
	//测试用
	public static void main(String[] args) throws ConnectException {
		/*try {
			// 建议都用/左斜杠这种，左斜杠是windows和linux通用的
			new WordPdfUtils().wordConverterToPdf("D:/111ccc.docx");
		} catch (IOException e) {
			System.out.println("word转换成pdf时出错");
			e.printStackTrace();
		}*/
	

	}
}
